//
//  EvaluCalculator.m
//  Rong360
//
//  Created by tianliwei on 16/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "EvaluCalculator.h"
#import "Config.h"
@implementation EvaluCalculator

- (void)beginCalculate{
    CGFloat trueLoan;
    CGFloat cmin_hous;
    CGFloat cmax_hous;
    self.grate = [Config shareConfig].jizhun_gjj;
    self.brate = [Config shareConfig].jizhun_house;
    CGFloat gR = ([self.grate floatValue] / 1200.0); //公积金月利率
    CGFloat bR = ([self.brate floatValue] / 1200.0); //商贷月利率
    CGFloat deadline = [self.deadline floatValue];
    CGFloat N = (deadline * 12.0);//还款期数
    CGFloat cmon_max = [self.cmon_max floatValue];//每月最高购房支出
    CGFloat cmax = [self.cmax floatValue] * 10000.0;//首付最多可支付
    CGFloat cmin = [self.cmin floatValue] * 10000.0;//首付最少可支付
    CGFloat p = [self.gloan floatValue] * 10000.0;//公积金可贷额度
    CGFloat cmonth = (p * gR * pow((1.0 +gR),N)) / (pow((1.0 +gR),N)-1);//月供
    if (round(cmonth) > round(cmon_max)) {
        trueLoan = (cmon_max * pow((1.0 +gR),N) - cmon_max) / (gR * pow((1.0 +gR),N));
    }else if (round(cmonth) == round(cmon_max)){
        trueLoan = p;
    }else{
        trueLoan = p + ((cmon_max - cmonth) * (pow((1.0 + bR * 0.85),N) - 1)) / (bR * 0.85 * pow((1 + bR * 0.85),N));
    }
    
    if (cmin / 3.0 * 7.0 > trueLoan) {
        cmin_hous = cmin + trueLoan;
    }else{
        cmin_hous = cmin / 3.0 * 10;
    }
    
    if (cmax / 3 * 7 > trueLoan) {
        cmax_hous = cmax + trueLoan;
    }else{
        cmax_hous = cmax / 3.0 * 10.0;
    }
    self.cmin_house = [NSString stringWithFormat:@"%d",(int)cmin_hous/10000];
    self.cmax_house = [NSString stringWithFormat:@"%d",(int)cmax_hous/10000];
}
@end
